<?php
/*
Template Name: Contact Form
Version: 1.0

Required form fields:

action: Must be the value "send"
to: Which email address to send form to
email: From email address
subject: Email subject line
thankyou: Thank you message after the form is sent

*/
global $Silas, $SilasConfig;

$validemails = array_merge($SilasConfig['mailValidEmails'], array(get_option('admin_email')));

if ($_POST['action'] == 'send') {
    $from = get_option('admin_email'); 
    $fromName = get_option('blogname'); 
    $host = $SilasConfig['mailHost'] ? $SilasConfig['mailHost'] : 'mail.dreamhost.com'; 
    $subject = get_option('blogname') ." - ". $_POST["subject"];
    $body = get_option('blogname') . " - Contact Form\n\n".
        "----------------------------------------------------------------------\n\n";
    foreach ($_POST as $name => $value) {
        if ($name != 'to' &&
            $name != 'subject' &&
            $name != 'action' &&
            $name != 'thankyou') {
        $body .= $name . " : " . $value . "\n";
        }
    }
    $body .= "----------------------------------------------------------------------\n\n".
    "Date: ".date("F j, Y, g:i a")."\n".
    "IP Address: ".$_SERVER['REMOTE_ADDR']."\n";
    
    $replyTo = $_POST['email'];
    $to = $_POST['to'];
    $headers = 'Reply-to: '.$replyTo."\r\n";
    /*
    require(dirname(__FILE__).'/phpmailer/class.phpmailer.php'); 
    $mail = new PHPMailer(); 

    $mail->From = $from 
    $mail->FromName = $fromName
    $mail->Host = $host; 
    $mail->Mailer = "smtp";
    
    $mail->Subject = $subject;
    $mail->Body = $body;
    
    if ($_POST['email']) {
        $mail->AddReplyTo($replyTo);
    }
    $mail->AddAddress($to); 

    $error_code = 0;
    $error_msg = "";
    if (in_array($_POST['to'], $validemails)) {
        $mail->Send();
    } else {
        $error_msg = "Not allowed to send emails to ".$_POST['to'].
            "<br />See <em>template-config.php</em> for valid email addresses";
    }


    if ($mail->IsError()) {
        $error_msg = $mail->ErrorInfo;
    }
    */
    if (in_array($_POST['to'], $validemails)) {
        if (!@wp_mail($to, $subject, $body, $headers)) {
            $error_msg = "There was an error processing the form. Please try again in a few seconds.";
        }
    } else {
        $error_msg = "Not allowed to send emails to ".$_POST['to'].
            "<br />See <em>template-config.php</em> for valid email addresses";
    }
    get_header();
    //$Silas->printBreadcrumb();
    
    ?>
<div class="tt-entry silas_content">
<?if ($error_msg):?>
    <h2>Opps!</h2>
    <div class="tt-contents">
    <p>There was an error while submitting your form. Please go back and try again.</p>
    <p class="error">Error: <strong><?=$error_msg?></strong></p>
    </div>
<?else:?>
    <h2>Thankyou!</h2>
    <div class="tt-contents">
    <?if ($_POST['thankyou']):?>
        <p><?=$_POST['thankyou']?></p>
    <?endif;?>
    <p>Back to <a href="<?bloginfo('url');?>"><?bloginfo('blogname');?></a></p>
	</div>
<?endif;?>
</div>
    <?
    
    get_footer();
    
} else {
    get_header();
    //$Silas->printBreadcrumb();
?>
<div class="tt-entry silas_content">
    
<?if (have_posts()) : while (have_posts()) : the_post(); ?>
    <h2><?the_title(); ?></h2>
    <div class="tt-contents">
    <? the_content(''); ?>
    </div>
<?endwhile; endif;?>

</div>
<?
    get_footer();
}
?>
